﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MG_POS_PC.Model;
using MG_POS_PC.Util;
using System.Data;

namespace MG_POS_PC.DAL
{

    public class UserDao
    {
        public UserDao()
        {
        }

        public bool Exists(int us_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_USER_SETTING");
            strSql.Append(" where ");
            strSql.Append(" us_id = @us_id  ");
            SqlParameter[] parameters = { new SqlParameter("@us_id", SqlDbType.Int, 4) };
            parameters[0].Value = us_id;

            return DBHelper.Exists(strSql.ToString(), parameters);
        }

        public void Add(User user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_USER_SETTING(");
            strSql.Append("us_login_id,us_pwd,us_info,us_active,us_color,us_manager");
            strSql.Append(") values (");
            strSql.Append("@us_login_id,@us_pwd,@us_info,@us_active,@us_color,@us_manager");
            strSql.Append(") ");

            SqlParameter[] parameters = {
			                     
                        new SqlParameter("@us_login_id", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@us_pwd", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@us_info", SqlDbType.VarChar,30) ,            
                        new SqlParameter("@us_active", SqlDbType.VarChar,1) ,            
                        new SqlParameter("@us_color", SqlDbType.Decimal,5) ,            
                        new SqlParameter("@us_manager", SqlDbType.VarChar,20)             
              
            };

            
            parameters[0].Value = user.us_login_id;
            parameters[1].Value = user.us_pwd;
            parameters[2].Value = user.us_info;
            parameters[3].Value = user.us_active;
            parameters[4].Value = user.us_color;
            parameters[5].Value = user.us_manager;
            DBHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public bool Update(User user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_USER_SETTING set ");

            
            strSql.Append(" us_login_id = @us_login_id , ");
            strSql.Append(" us_pwd = @us_pwd , ");
            strSql.Append(" us_info = @us_info , ");
            strSql.Append(" us_active = @us_active , ");
            strSql.Append(" us_color = @us_color , ");
            strSql.Append(" us_manager = @us_manager  ");
            strSql.Append(" where us_id=@us_id  ");

            SqlParameter[] parameters = {
			                
                        new SqlParameter("@us_login_id", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@us_pwd", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@us_info", SqlDbType.VarChar,30) ,            
                        new SqlParameter("@us_active", SqlDbType.VarChar,1) ,            
                        new SqlParameter("@us_color", SqlDbType.Decimal,5) ,            
                        new SqlParameter("@us_manager", SqlDbType.VarChar,20)             
              
            };

            
            parameters[0].Value = user.us_login_id;
            parameters[1].Value = user.us_pwd;
            parameters[2].Value = user.us_info;
            parameters[3].Value = user.us_active;
            parameters[4].Value = user.us_color;
            parameters[5].Value = user.us_manager;
            int rows = DBHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Delete(int us_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_USER_SETTING ");
            strSql.Append(" where us_id=@us_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@us_id", SqlDbType.Int,4)			};
            parameters[0].Value = us_id;


            int rows = DBHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public User GetUser(int us_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select us_id, us_login_id, us_pwd, us_info, us_active, us_color, us_manager  ");
            strSql.Append("  from T_USER_SETTING ");
            strSql.Append(" where us_id=@us_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@us_id", SqlDbType.Int,4)			};
            parameters[0].Value = us_id;


            User user = new User();
            DataSet ds = DBHelper.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["us_id"].ToString() != "")
                {
                    user.us_id = int.Parse(ds.Tables[0].Rows[0]["us_id"].ToString());
                }
                user.us_login_id = int.Parse(ds.Tables[0].Rows[0]["us_login_id"].ToString());
                user.us_pwd = ds.Tables[0].Rows[0]["us_pwd"].ToString();
                user.us_info = ds.Tables[0].Rows[0]["us_info"].ToString();
                user.us_active = ds.Tables[0].Rows[0]["us_active"].ToString();
                if (ds.Tables[0].Rows[0]["us_color"].ToString() != "")
                {
                    user.us_color = int.Parse(ds.Tables[0].Rows[0]["us_color"].ToString());
                }
                user.us_manager = ds.Tables[0].Rows[0]["us_manager"].ToString();

                return user;
            }
            else
            {
                return null;
            }
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_USER_SETTING ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DBHelper.Query(strSql.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM T_USER_SETTING ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DBHelper.Query(strSql.ToString());
        }
    }
}
